package com.jiayuan.cn.energy.controller;

import cn.hutool.json.JSONObject;
import com.jiayuan.cn.energy.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.jiayuan.cn.energy.util.RSAUtil;

@Slf4j
@RestController
public class ApiAction {

    @Resource
    UserService userService;

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World! This is 储能数据管理系统API";
    }

    @RequestMapping("/getPublicKey")
    public String getPublicKey() throws Exception {
        log.info("getPublicKey:{}",userService.getPublicKey());
        return userService.getPublicKey();
    }

    @PostMapping(value = "/loginRequest")
    @ResponseBody
    public String loginRequest(@RequestBody JSONObject json) {
        String username = json.getStr("username");
        String password = json.getStr("password");
        System.out.println(username);
        System.out.println(password);
        String res = null;
        try {
            res = RSAUtil.decryptByPrivateKey(userService.getPublicKey(), password);
            //这里就是解密后的密码了
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
